import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 东莞呵呵
 * Date:2022-06-18
 * Time:16:08
 */
public class TestDemo2 {
    public static void main(String[] args) {
        int[] nums1={1,1,2};
        int[] nums2={1,2,3};
        List<List<Integer>> ret=kSmallestPairs(nums1,nums2,2);
        System.out.println(ret);
    }
    public static List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
        PriorityQueue<List<Integer>> priorityQueue=new PriorityQueue<>(k, new Comparator<List<Integer>>() {
            @Override
            public int compare(List<Integer> o1, List<Integer> o2) {
                return (o2.get(0)+ o2.get(1))-(o1.get(0)+o1.get(1));
            }
        });
        for (int i = 0; i < Math.min(nums1.length,k); i++) {
            for(int j=0;j<Math.min(nums2.length,k);j++){
                if(priorityQueue.size()<k){
                    List<Integer> list1=new ArrayList<>();
                    list1.add(nums1[i]);
                    list1.add(nums2[j]);
                    priorityQueue.offer(list1);
                }else{
                    int top=priorityQueue.peek().get(0)+priorityQueue.peek().get(1);
                    int tmp=nums1[i]+nums2[j];
                    if(top>=tmp){
                        List<Integer> list1=new ArrayList<>();
                        list1.add(nums1[i]);
                        list1.add(nums2[j]);
                        priorityQueue.poll();
                        priorityQueue.offer(list1);
                    }
                }
            }

        }
        List<List<Integer>> ret=new ArrayList<>();
        for (int i = 0; i < k && !priorityQueue.isEmpty(); i++) {
            ret.add(priorityQueue.poll());
        }
        return ret;
    }
}
